package org.xqh.study.pattern.partterns.abstractDocument;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

public abstract class AbstractDocument implements Document {

    private final Map<String,Object> properties;


    protected AbstractDocument(Map<String, Object> properties) {
        Objects.requireNonNull(properties,"properties map is required");
        this.properties = properties;
    }

    @Override
    public void put(String key, Object value) {
        properties.put(key,value);
    }

    @Override
    public Object get(String key) {
        return properties.get(key);
    }

    @Override
    public <T> Stream<T> children(String key, Function<Map<String, Object>, T> construction) {
        Optional<List<Map<String,Object>>> any=Stream.of(get(key)).filter(el-> el != null).map(el-> (List<Map<String,Object>>)el).findAny();
        return any.isPresent() ? any.get().stream().map(construction) : Stream.empty();
    }
}
